var prefix = api + "/job/sys-job";

$(function () {
  var statusDatas = dictByType('job_status');
  var executeDatas = dictByType('job_execute_status');
  var typeDatas = dictByType('job_type');
  var methodTypeList = dictByType('method_type');
  var policyeDatas = dictByType('misfire_policy');
  renderDictFilter('#jobStatus', 'job_status')
  renderDictFilter('#jobExecuteStatus', 'job_execute_status')
  var addFlag = permissions.job_sys_job_add
  var editFlag = permissions.job_sys_job_edit
  var deleteFlag = permissions.job_sys_job_del
  var shutdownFlag = permissions.job_sys_job_shutdown_job
  var startFlag = permissions.job_sys_job_start_job
  // var refreshFlag = permissions.job_sys_job_refresh_job
  var runFlag = permissions.job_sys_job_run_job
  //没有新增权限的话删除新增按钮
  if (!addFlag) {
    $('#addBtn').remove()
  }
  if (!startFlag) {
    $('#startBtn').remove()
  }
  if (!shutdownFlag) {
    $('#shutdownBtn').remove()
  }
  var options = {
    url: prefix + "/page",
    updateUrl: "/pages/monitor/job/jobEdit",
    removeUrl: prefix + "/{id}",
    // sortName: "createTime",
    // sortOrder: "desc",
    modalName: "调度任务",
    columns: [{
      title: "序号",
      formatter: function (value, row, index) {
        return $.table.serialNumber(index);
      }
    }, {
      field: 'jobName',
      title: '任务名称',
      // formatter: function (value, row, index) {
      // 	return '<a href="javascript:$.operate.view(' + "'" + row.jobId + "'" + ')">' + value + '</a>';
      // }
    }, {
      field: 'jobGroup',
      title: '任务组名'
    }, {
      field: 'jobStatus',
      title: '任务状态',
      formatter: function (value, row, index) {
        return $.table.selectDictLabel(statusDatas, value);
      }
    }, {
      field: 'jobExecuteStatus',
      title: '执行状态',
      formatter: function (value, row, index) {
        return $.table.selectDictLabel(executeDatas, value);
      }
    }, {
      field: 'startTime',
      title: '首次执行时间',
			sortable: true,
    }, {
      field: 'previousTime',
      title: '上次执行时间',
			sortable: true,
    }, {
      field: 'nextTime',
      title: '下次执行时间',
			sortable: true,
    }, {
      field: 'jobType',
      title: '类型',
      formatter: function (value, row, index) {
        return $.table.selectDictLabel(typeDatas, value);
      }
    }, {
      field: 'methodType',
      title: 'HTTP请求类型',
      formatter: function (value, row, index) {
        return $.table.selectDictLabel(methodTypeList, value);
      }
    }, {
      field: 'executePath',
      title: '执行路径'
    }, {
      field: 'className',
      title: '执行文件'
    }, {
      field: 'methodName',
      title: '执行方法'
    }, {
      field: 'methodParamsValue',
      title: '执行参数值'
    }, {
      field: 'cronExpression',
      title: 'cron表达式'
    }, {
      field: 'misfirePolicy',
      title: '错失执行策略',
      formatter: function (value, row, index) {
        return $.table.selectDictLabel(policyeDatas, value);
      }
    }, {
      field: 'remark',
      title: '备注信息',
    }, {
      title: '操作',
      formatter: function (value, row, index) {
        var actions = [];
        if (editFlag && row.jobStatus != '2') {
          actions.push('<a class="btn btn-success btn-xs " href="javascript:void(0)" onclick="$.operate.edit(\'' + row.jobId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
        }
        //                actions.push('<a class="btn btn-danger btn-xs ' + deleteFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.jobId + '\')"><i class="fa fa-remove"></i>删除</a> ');
        if (deleteFlag) {
          actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="removeQuartz(' + row.jobId + ',' + row.jobStatus + ')"><i class="fa fa-remove"></i>删除</a> ');
        }
        var more = [];
        if (runFlag) {
          more.push("<a class='btn btn-default btn-xs ' href='javascript:void(0)' onclick='runJob(" + row.jobId + "," + row.jobStatus + ")'><i class='fa fa-play-circle-o'></i> 执行一次</a> ");
        }
        if (startFlag) {
          more.push("<a class='btn btn-default btn-xs ' href='javascript:void(0)' onclick='startJob(" + row.jobId + ")'><i class='glyphicon glyphicon-play'></i>启动</a> ");
        }
        if (shutdownFlag) {
          more.push("<a class='btn btn-default btn-xs ' href='javascript:void(0)' onclick='shutdownJob(" + row.jobId + ")'><i class='glyphicon glyphicon-pause'></i>暂停</a> ");
        }
        more.push("<a class='btn btn-default btn-xs' href='javascript:void(0)' onclick='jobLog(" + row.jobId + ")'><i class='fa fa-list'></i>调度日志</a>");
        actions.push('<a class="btn btn-info btn-xs" role="button" data-container="body" data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" data-content="' + more.join('') + '"><i class="fa fa-chevron-circle-right"></i>更多操作</a>');
        return actions.join('');
      }
    }]
  };
  $.table.init(options);
});

/* 暂停全部任务 */
function shutdownAllJob() {
  $.modal.confirm("确认要暂停全部任务吗？", function () {
    $.operate.post(prefix + "/shutdown-jobs");
  })
}

/* 启动全部任务 */
function startAllJob() {
  $.modal.confirm("确认要启动全部任务吗？", function () {
    $.operate.post(prefix + "/start-jobs");
  })
}

/* 重置全部任务 */
// function refreshJob() {
// 	$.modal.confirm("确认要重置全部任务吗？", function() {
// 		$.operate.post(prefix + "/refresh-jobs");
//     })
// }

/* 立即执行一次 */
function runJob(jobId, jobStatus) {
  if (jobStatus == 1) {
    $.modal.alertWarning("请先启动后再执行");
    return
  }
  $.modal.confirm("确认要立即执行一次任务吗？", function () {
    $.operate.post(prefix + "/run-job/" + jobId);
  })
}

/* 启动任务 */
function startJob(jobId) {
  $.modal.confirm("确认要启动任务吗？", function () {
    $.operate.post(prefix + "/start-job/" + jobId);
  })
}

/* 暂停任务 */
function shutdownJob(jobId) {
  $.modal.confirm("确认要暂停任务吗？", function () {
    $.operate.post(prefix + "/shutdown-job/" + jobId);
  })
}

/* 调度日志查询 */
function jobLog(jobId) {
  // var url = prefix + '/job-log';
  var url = "/pages/monitor/job/jobLogDetail"
  if ($.common.isNotEmpty(jobId)) {
    url += '?jobId=' + jobId;
  }
  $.modal.openTab("调度日志", url);
}

/* 删除任务 */
function removeQuartz(jobId, jobStatus) {
  if (jobStatus == 2) {
    $.modal.alertWarning("运行中定时任务不可删除");
    return
  }
  $.operate.remove(jobId)
}